package 单调栈;

import org.junit.Test;

/**
 * @author aodre , QQ : 480029069
 * @date 2023/7/21 16:26
 */
public class lc556下一个更大的元素III {


    public int solution(int num){
        char[] chars = String.valueOf(num).toCharArray();
        dfs(0,chars,new boolean[chars.length],0,num);
        return min == Long.MAX_VALUE ? -1 : (int)min ;
    }


    private long min = Long.MAX_VALUE;

    public void dfs(int index,char []nums,boolean []used,long path,int num){
        if(index == nums.length){
            if(path > num  && path <= Integer.MAX_VALUE){
                min = Math.min(min,path);
            }
            return;
        }

        for(int i = 0;i < nums.length;i++){
            if(!used[i]){
                used[i] = true;
                dfs(index + 1,nums,used,path + (nums[i] - '0') * (long)Math.pow(10,index),num);
                used[i] = false;
            }
        }
    }


    @Test
    public void test(){

//        System.out.println(solution(21));
//        System.out.println(solution(12));
        System.out.println(solution(2147483476));
    }

}
